import java.util.Arrays;

public class TextHeap {
    public int[] arr;
    public int size;

    public TextHeap(int[] arr) {
        this.arr = arr;
        size = arr.length;
    }

    public void createheap() {
        for (int parent = (size - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(parent, size);
        }
    }

    private void shiftDown(int parent, int len) {
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if (arr[parent] < arr[child]) {
                int tmp = arr[parent];
                arr[parent] = arr[child];
                arr[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public void shiftUp(int child){
        int parent=(child-1)/2;
        while(child>0){
            if(arr[child]>arr[parent]){
                int tmp = arr[parent];
                arr[parent] = arr[child];
                arr[child] = tmp;
                child=parent;
                parent=(child-1)/2;
            }else {
                break;
            }
        }
    }

    public void offer(int val) {
        if (isfull()) {
            arr = Arrays.copyOf(arr, arr.length * 2);
        }
        arr[size++] = val;

    }

    public boolean isfull() {
        return arr.length == size;
    }
    public boolean empty() {
        return 0 == size;
    }
    public void pop(){
        if (empty()){
            return;
        }
        int tmp = arr[0];
        arr[0] = arr[size-1];
        arr[size-1] = tmp;
        size--;
        shiftDown(0,size);
    }
}
